package com.jvyou.app.ds.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jvyou.app.common.domain.dto.RoleDto;
import com.jvyou.app.common.domain.entity.Role;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.domain.model.Result;
import com.jvyou.app.common.enums.HttpCodeEnum;
import com.jvyou.app.ds.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 角色信息表(Role)表控制层
 *
 * @author 橘柚
 * @since 2025-03-05 14:11:41
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 查询全部数据
     *
     * @return 列表
     */
    @GetMapping("/list")
    @SaCheckPermission("system:role:list")
    public Result<List<Role>> list(@ModelAttribute RoleDto roleDto) {
        return Result.success(roleService.listRole(roleDto));
    }


    /**
     * 分页查询数据
     *
     * @return 列表
     */
    @GetMapping("/page")
    @SaCheckPermission("system:role:page")
    public Result<PageResult<Role>> page(@ModelAttribute RoleDto roleDto) {
        return Result.success(roleService.pageRole(roleDto));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @SaCheckPermission("system:role:get")
    public Result<Role> selectOne(@PathVariable Serializable id) {
        return Result.success(roleService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param role 实体对象
     * @return 新增结果
     */
    @PostMapping
    @SaCheckPermission("system:role:insert")
    public Result<Role> insert(@RequestBody Role role) {
        boolean success = roleService.save(role);
        return success ? Result.success(role) : Result.error(HttpCodeEnum.ADD_FAIL);
    }

    /**
     * 修改数据
     *
     * @param role 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateById")
    @SaCheckPermission("system:role:update")
    public Result<Role> updateById(@RequestBody Role role) {
        boolean success = roleService.updateById(role);
        return success ? Result.success(role) : Result.error(HttpCodeEnum.UPDATE_FAIL);
    }


    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("system:role:delete")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = roleService.removeById(id);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.DELETE_FAIL);
    }

    /**
     * 分配权限
     *
     * @param roleDto roleDto
     * @return Result
     */
    @SaCheckPermission("system:role:assignPermissions")
    @PostMapping("/assignPermissions")
    public Result<Boolean> assignPermissions(@RequestBody RoleDto roleDto) {
        log.info("分配权限参数：{}", roleDto);
        if (roleDto.getId() == null || roleDto.getId() == 0 || roleDto.getMenuIds() == null) {
            return Result.error(HttpCodeEnum.ARGUMENT_VALID_ERROR, "角色ID或者权限列表不能为空");
        }
        return Result.success(roleService.assignPermissions(roleDto.getId(), roleDto.getMenuIds()));
    }
}

